﻿using BCCommon;
using BCEntity.Sim.Admin.TreePermission;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data;

namespace BCData.Sim.Admin.TreePermission
{
    public class TreePermissionConfigData : ITreePermissionConfigData
    {
        public readonly IDatabaseContext database;

        public const string QUERY = @"SELECT TreePermissionConfigId,PermissionName,PermissionType,PermissionCode,PermissionPathCode,ParentPermissionPathCode,
                                    PermissionLevel,PermissionOrder,PagePathCode,Icon,IsShow,CreateTime FROM TreePermissionConfig";

        public TreePermissionConfigData(IDatabaseContext database)
        {
            this.database = database;
        }

        public async Task<bool> CheckExists(string permissionCode)
        {
            return await database.Slave.QueryFirstOrDefaultAsync<TreePermissionConfigEntity>($"{QUERY} WHERE PermissionCode = ?PermissionCode", new { permissionCode }) != null;
        }

        public async Task<TreePermissionConfigEntity> InsertAsync(TreePermissionConfigEntity entity)
        {
            return await database.Master.InsertAsync(entity);
        }

        public async Task<TreePermissionConfigEntity> GetEntityAsync(long treePermissionConfigId)
        {
            return await database.Slave.QueryFirstOrDefaultAsync<TreePermissionConfigEntity>($"{QUERY} WHERE TreePermissionConfigId = ?TreePermissionConfigId", new { treePermissionConfigId });
        }
        public async Task<TreePermissionConfigEntity> GetEntityAsync(string parentPermissionPathCode)
        {
            return await database.Slave.QueryFirstOrDefaultAsync<TreePermissionConfigEntity>($"{QUERY} WHERE PermissionPathCode = ?PermissionPathCode", new { PermissionPathCode = parentPermissionPathCode });
        }

        public async Task<TreePermissionConfigEntity> Update(TreePermissionConfigEntity entity)
        {
            return await database.Master.UpdateAsync(entity);
        }

        public async Task<int> UpdateShow(long treePermissionConfigId, bool isShow)
        {
            return await database.Master.ExecuteAsync($"UPDATE TreePermissionConfig SET IsShow=?IsShow WHERE TreePermissionConfigId = ?TreePermissionConfigId", new { treePermissionConfigId, isShow }, database.Transaction);
        }

        public async Task<int> Delete(long treePermissionConfigId)
        {
            return await database.Master.ExecuteAsync($"DELETE FROM TreePermissionConfig WHERE TreePermissionConfigId = ?TreePermissionConfigId", new { treePermissionConfigId }, database.Transaction);
        }

        public async Task<(IEnumerable<TreePermissionConfigEntity>, int)> QueryAsync(string permissionName, string permissionCode, string permissionPathCode, string parentPermissionPathCode, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (!string.IsNullOrWhiteSpace(permissionName))
            {
                wheres.Add("PermissionName LIKE  CONCAT('%',CONCAT(?PermissionName,'%'))");
                parameters.Add("?PermissionName", permissionName, DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(permissionCode))
            {
                wheres.Add("PermissionCode LIKE CONCAT('%',CONCAT(?PermissionCode,'%'))");
                parameters.Add("?PermissionCode", permissionCode, DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(permissionPathCode))
            {
                wheres.Add("PermissionPathCode LIKE CONCAT('%',CONCAT(?PermissionPathCode,'%'))");
                parameters.Add("?PermissionPathCode", permissionPathCode, DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(parentPermissionPathCode))
            {
                wheres.Add("ParentPermissionPathCode = ?ParentPermissionPathCode");
                parameters.Add("?ParentPermissionPathCode", parentPermissionPathCode, DbType.AnsiString);
            }
            string sql = string.Format(@"{0} {1} ORDER BY TreePermissionConfigId {2} LIMIT {3},{4};
                                    SELECT COUNT(1) FROM TreePermissionConfig {1}",
                                    QUERY,
                                    wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : string.Empty,
                                    ascending.HasValue && ascending.Value ? "ASC" : "DESC",
                                    (pageIndex - 1) * pageSize,
                                    pageSize);
            return await database.Slave.QueryPageableListAsync<TreePermissionConfigEntity>(sql, parameters);
        }

        public async Task<IEnumerable<TreePermissionConfigEntity>> GetList(string permissionName, string permissionCode, string permissionPathCode, string parentPermissionPathCode)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (!string.IsNullOrWhiteSpace(permissionName))
            {
                wheres.Add("PermissionName = ?PermissionName");
                parameters.Add("?PermissionName", permissionName, DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(permissionCode))
            {
                wheres.Add("PermissionCode = ?PermissionCode");
                parameters.Add("?PermissionCode", permissionCode, DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(permissionPathCode))
            {
                wheres.Add("PermissionPathCode = ?PermissionPathCode");
                parameters.Add("?PermissionPathCode", permissionPathCode, DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(parentPermissionPathCode))
            {
                wheres.Add("ParentPermissionPathCode = ?ParentPermissionPathCode");
                parameters.Add("?ParentPermissionPathCode", parentPermissionPathCode, DbType.AnsiString);
            }
            string sql = string.Format("{0} {1} ORDER BY PermissionPathCode, PermissionLevel", QUERY, wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : string.Empty);
            return await database.Slave.QueryAsync<TreePermissionConfigEntity>(sql, parameters);
        }

    }
}
